package com.itheima.area.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.area.dao.CropDao;
import com.itheima.area.dao.DeviceDao;
import com.itheima.area.dao.FieldDao;
import com.itheima.area.pojo.CustFarmland;
import com.itheima.area.pojo.CustFarmlandCrop;
import com.itheima.area.pojo.CustFarmlandDevice;
import com.itheima.area.service.FieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @PackageName com.itheima.area.service.impl
 * @ProjectName Ceres-Work
 * @Created by Chanly on 2020/10/14 9:52
 **/
@Service
@Transactional
public class FieldServiceImpl implements FieldService {
    @Autowired
    private FieldDao fieldDao ;
    @Autowired
    private CropDao cropDao ;
    @Autowired
    private DeviceDao deviceDao ;

    @Override
    public List<CustFarmland> findAll(Long id) {

        return fieldDao.findAll(id);
    }

    /**
     * 根据id删除设备与农作物
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        fieldDao.deleteCropByFarmId(id);
        fieldDao.deleteDeviceByFarmId(id);
    }

    /**
     * 添加片区
     * @param custFarmland
     * @param custFarmlandDevice
     * @param custFarmlandCrop
     */
    @Override
    public void add(CustFarmland custFarmland,CustFarmlandCrop custFarmlandCrop, CustFarmlandDevice custFarmlandDevice) {
    fieldDao.addFarmLand(custFarmland);
    fieldDao.addFarmlandCrop(custFarmlandCrop);
    fieldDao.addFarmlandDevice(custFarmlandDevice);
    }

    /**
     * 修改片区
     * @param custFarmland
     * @param custFarmlandDevice
     * @param custFarmlandCrop
     * @param id
     */
    @Override
    public void update(CustFarmland custFarmland, CustFarmlandDevice custFarmlandDevice, CustFarmlandCrop custFarmlandCrop, Long id) {
     fieldDao.updateFarmlandById(custFarmland.getFid(),custFarmland.getShape(),custFarmland.getSize(),custFarmland.getStatus(),custFarmland.getCode(),id);
     fieldDao.updateCropByFarmlandId(custFarmlandCrop.getCropLandId(),custFarmlandCrop.getStage(),custFarmlandCrop.getAttention(),custFarmlandCrop.getImg(),custFarmlandCrop.getStartTime(),custFarmlandCrop.getEndTime(),custFarmlandCrop.getName(),id);
     fieldDao.updateDeviceByFarmlandId(custFarmlandDevice.getDid(),custFarmlandDevice.getDevLandId(),custFarmlandDevice.getCode(),custFarmlandDevice.getOperation(),custFarmlandDevice.getInstall(),custFarmlandDevice.getLocation(),id);
    }

    @Override
    public Page<CustFarmland> findPage(Map searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        return (Page<CustFarmland>)fieldDao.selectByExample(example);
    }

    private Example createExample(Map searchMap) {
        Example example=new Example(CustFarmland.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 所属农场
            if(searchMap.get("fid")!=null && !"".equals(searchMap.get("fid"))){
                criteria.andLike("fid","%"+searchMap.get("fid")+"%");
            }
            // 大小
            if(searchMap.get("size")!=null && !"".equals(searchMap.get("size"))){
                criteria.andLike("size","%"+searchMap.get("size")+"%");
            }
            // 形状
            if(searchMap.get("shape")!=null && !"".equals(searchMap.get("shape"))){
                criteria.andLike("shape","%"+searchMap.get("shape")+"%");
            }
            // 编号
            if(searchMap.get("code")!=null && !"".equals(searchMap.get("code"))){
                criteria.andLike("code","%"+searchMap.get("code")+"%");
            }

            // 片区状态
            if(searchMap.get("status")!=null && !"".equals(searchMap.get("status"))){
                criteria.andEqualTo("status",searchMap.get("status"));
            }

        }
        return example;
    }



}
